﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;

namespace CCF20Model
{

    public class CCFCount : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private int? _zicAvailable;
        public int? ZicAvailable
        {
            get
            {
                return this._zicAvailable;
            }
            set
            {
                if (value != this._zicAvailable)
                {
                    this._zicAvailable = value;
                    NotifyPropertyChanged("ZicAvailable");
                }
            }

        }
        private int _faultCount;
        public int FaultCount
        {
            get
            {
                return this._faultCount;
            }
            set
            {
                if (value != this._faultCount)
                {
                    this._faultCount = value;
                    NotifyPropertyChanged("FaultCount");
                }
            }

        }

        private int _crashCount;
        public int CrashCount
        {
            get
            {
                return this._crashCount;
            }
            set
            {
                if (value != this._crashCount)
                {
                    this._crashCount = value;
                    NotifyPropertyChanged("CrashCount");
                }
            }

        }
        private string _lastZICUpdate;
        public string LastZICUpdate
        {
            get
            {
                return this._lastZICUpdate;
            }
            set
            {
                if (value != this._lastZICUpdate)
                {
                    this._lastZICUpdate = value;
                    NotifyPropertyChanged("LastZICUpdate");
                }
            }

        }
        private int _maintenanceCount;
        public int MaintenanceCount
        {
            get
            {
                return this._maintenanceCount;
            }
            set
            {
                if (value != this._maintenanceCount)
                {
                    this._maintenanceCount = value;
                    NotifyPropertyChanged("MaintenanceCount");
                }
            }

        }


        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

    }
    
    public class ZICMonitorModel
    {
        CCF20Entities ccf20Ent;
        CCFCount myMonitor;
        static ZIC.ZicWebService _ZICSvc;
        static ZICJava.ZicWebService _ZICJava;
        static LoginInfo _ZICLoginInfo;
        public ZICMonitorModel()
        {
        }
        public static string ZICUrl
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                CompanyProfile profile = ccf20Ent.CompanyProfiles.FirstOrDefault();
            
                if (profile != null)
                {
                    return profile.ZICUrl;
                }
                else
                {
                    return "http://218.249.201.36:81/zicwebservice.asmx";
                }
            }
        }
        public static void  UpdateZICUrl(string url,bool isJava)
        {
            if (url == null || url.Trim().Length == 0) return;
            CCF20Entities ccf20Ent = new CCF20Entities();

            CompanyProfile profile = ccf20Ent.CompanyProfiles.FirstOrDefault();

            if (profile == null)
            {
                profile = new CompanyProfile();
                ccf20Ent.CompanyProfiles.AddObject(profile);
                
            }
                
            profile.ZICUrl = url;
            profile.IsJava = isJava;
            ccf20Ent.SaveChanges();
        }
        public CCFCount GetCount()
        {
            ccf20Ent = new CCF20Entities();
            myMonitor = new CCFCount();
            myMonitor.FaultCount = ccf20Ent.Vehicles.Count(v => v.Faults.Count(f => !f.IsConfirmed && f.IsActive && f.FaultType == "故障" && f.Vehicle.Devices.FirstOrDefault().IsRegistered == true) > 0);
            myMonitor.CrashCount = ccf20Ent.Vehicles.Count(v => v.Faults.Count(f => f.IsActive && f.FaultType == "撞车" && f.Vehicle.Devices.FirstOrDefault().IsRegistered == true)>0);
            myMonitor.MaintenanceCount = ccf20Ent.Vehicles.Count(v=>v.VehicleMaintenanceHistories.Count(m => m.IsActive && m.Vehicle.Devices.FirstOrDefault().IsRegistered == true)>0);
            //myMonitor.ZicAvailable = 0;
            if (ccf20Ent.CompanyProfiles.FirstOrDefault().ZICUpdateTime != null)
            {
                myMonitor.LastZICUpdate = ccf20Ent.CompanyProfiles.FirstOrDefault().ZICUpdateTime.Value.ToString();
                //if (ccf20Ent.CompanyProfiles.FirstOrDefault().ZICAvailable == true) myMonitor.ZicAvailable = 1;
            }
            else
            {
                myMonitor.LastZICUpdate = null;
                
            }

            return myMonitor;
        }
        public static LoginInfo ZICLoginInfo
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                CompanyProfile profile = ccf20Ent.CompanyProfiles.FirstOrDefault();
                if ((profile == null) || profile.LoginName == null || profile.LoginPassword == null)
                {
                    throw new Exception("请先设定IDS 用户名和密码");
                }
                _ZICLoginInfo = new LoginInfo();
                _ZICLoginInfo.IDSName = profile.LoginName;
                _ZICLoginInfo.IDSPassword = profile.LoginPassword;
                return _ZICLoginInfo;
            }
        }
        public static ZIC.ZicWebService ZICSvc
        {
            get
            {
                _ZICSvc = new ZIC.ZicWebService();
                _ZICSvc.Url = ZICMonitorModel.ZICUrl;
                return _ZICSvc;
            }
             
        }
        public static ZICJava.ZicWebService ZICJava
        {
            get
            {
                _ZICJava = new ZICJava.ZicWebService();
                _ZICJava.Url = ZICMonitorModel.ZICUrl;
                return _ZICJava;
            }

        }
        public static bool IsJava
        {
            get
            {
                CCF20Entities ccf20Ent = new CCF20Entities();
                CompanyProfile profile = ccf20Ent.CompanyProfiles.FirstOrDefault();

                if ( profile != null && profile.IsJava != null)
                {
                    return (bool) profile.IsJava;
                }
                else
                {
                    return false;
                }

            }
        }
    }
}
